<template>
  <div class="box" style="height: 500px">
    <div class="xtx-slider" @mouseenter="clearTimer" @mouseleave="startTimer">
      <!-- 图片列表 -->
      <ul class="slider-body">
        <!--
          fade: 当fade类名存在 当前图片就显示 不存在就不显示
        -->
        <li
          class="slider-item"
          v-for="(item, i) in sliders"
          :key="i"
          :class="{ fade: curIndex === i }"
        >
          <img :src="item.imgUrl" alt="img" />
        </li>
      </ul>
      <!-- 切换轮播图按钮 -->
      <div class="xtx-slider__switch-box" :style="{ 'width': width }">
        <a href="javascript:;" v-on:click="switchSub">
          <span></span>
        </a>
        <a href="javascript:;" v-on:click="switchAdd">
          <span></span>
        </a>
      </div>
      <!-- 圆圈切换按钮 -->
      <div class="slider-indicator">
        <span
          v-for="(item, index) in sliders"
          :key="index"
          @click="curIndex = index"
          :class="{ active: curIndex === index }"
        ></span>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
export default {
  name: 'XtxSlider'
}
</script>

<script setup lang="ts">
/**
 * 目标：点击圆圈按钮 实现对应图片的切换
 * 思路：
 *   1. 图片和圆圈按钮数量是一样的 下标值是对应的
 *   2. 记录一下当前点击的是哪一项
 *   3. 需要根据记录下来的下标值 去配合:class 控制fade这个类名是否应该显示
 */
import { strictEqual } from 'assert';
import { ref, onMounted } from 'vue'

const props = defineProps({
  // 数据列表
  sliders: {
    type: Array,
    default: () => {
      return []
    }
  },
  // 是否自动轮播
  autoPlay: {
    type: Boolean,
    default: true
  },
  width: {
    type: String,
    default: '990px'
  }
})

const curIndex = ref(0)

// 声明一个存放定时器的数据
const timer = ref(null)
function clearTimer() {
  clearInterval(timer.value)
}
function startTimer() {
  // 开启定时器  每隔2s中修改一下curIndex的值
  initLoop()
}

function initLoop() {
  if (!props.autoPlay) {
    return false
  }
  // 开启定时器  每隔2s中修改一下curIndex的值
  timer.value = window.setInterval(() => {
    // 最大能到多大
    // 2  1 -> 2 从2设置为0
    curIndex.value++
    if (curIndex.value > props.sliders.length - 1) {
      curIndex.value = 0
    }
  }, 2000)
}

// 轮播图左切
const switchAdd = () => {

  if (curIndex.value >= props.sliders.length - 1) {
    curIndex.value = 0
  } else {
    curIndex.value++
  }
}
// 轮播图右切
const switchSub = () => {
  if (curIndex.value <= 0) {
    curIndex.value = props.sliders.length - 1
  } else {
    curIndex.value--
  }
}
onMounted(() => {
  initLoop()
})
</script>
<style scoped lang='less'>
.xtx-slider {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  &__switch-box {
    position: absolute;
    top: 45%;
    right: 0;
    height: 60px;
    z-index: 998;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    & :nth-child(1) span {
      transform: rotate(130deg);
    }
    & :nth-child(2) span {
      transform: rotate(-45deg);
    }
    & a {
      border-radius: 50%;
      padding-top: 15px;
      padding-left: 15px;
      width: 50px;
      height: 50px;
      background-color: #ccc;

      & span {
        display: block;
        width: 15px;
        height: 15px;
        border-bottom: 2px solid #000;
        border-right: 2px solid #000;
      }
    }
  }
  .slider {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .slider-btn {
      opacity: 1;
    }
  }
}
</style>